package key;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Register")
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;

	Log log = new Log();
	Database db = new Database();

	public void init(ServletConfig config) throws ServletException {
	}

	public Register() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String uidString = request.getParameter("uid");
		String uname = request.getParameter("uname");
		String pass = request.getParameter("pass");
		String dep = request.getParameter("dep");

		if (db.isExist(uname)) {
			response.sendRedirect("index.jsp");
			@SuppressWarnings("unused")
			Frame fr = new Frame("用户名已存在");
		} else {

			int nn = db.addUser(uidString, uname, pass, dep);

			System.out.println(uname + "注册成功，更新" + nn + "条数据");

			/* 生成页面 */
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			response.setCharacterEncoding("utf-8");
			out.println("<html>\n<head>\n<title>提示信息</title>\n</head>\n<body>");
			out.println("<script type='text/javascript'>\nconfirm('您已完成" + uname + "的注册')");
			out.println("window.location.href='index.jsp';\n</script>");
			out.println("</body>\n</html>");
			System.out.println(uname + "注册成功");

			// response.sendRedirect("index.jsp");
			// @SuppressWarnings("unused")
			// Frame fr = new Frame("注册成功");
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
